package org.example.chatroom.common.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LIUYANJIAO
 * Date: 2025-06-21
 * Time: 16:24
 */
@Slf4j
public class JWTUtils {
    //签名：6KRmxala3Y5Vhe/SD4NX20RNmyhVueBW5nUO0oyWs0E=
    private static String SECRET_ATRING="6KRmxala3Y5Vhe/SD4NX20RNmyhVueBW5nUO0oyWs0E=";
    private static Key key = Keys.hmacShaKeyFor(SECRET_ATRING.getBytes(StandardCharsets.UTF_8));

    /**
     * 加密
     * @param claims
     * @return 获得token
     */
    public static String genToken(Map<String,Object> claims){
        String compact = Jwts.builder()
                .setClaims(claims)
                .signWith(key)
                .compact();
        return compact;
    }

    /**
     * 解密
     * @param token
     * @return 获得信息
     */
    public static Claims parseToken(String token){
        JwtParser bulid = Jwts.parserBuilder()
                .setSigningKey(key)
                .build();
        Claims claims = bulid.parseClaimsJws(token).getBody();
        return claims;
    }


}
